package com.wq.wrongquestion.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.wq.wrongquestion.entity.geo.GeographyEntity;
import com.wq.wrongquestion.service.geo.GeographyService;
import com.wq.wrongquestion.utils.FileUploadUtils;

@Controller
@RequestMapping("/geo")
public class GeographyController {
    /************************************************Geography Start************************************************/
    @Autowired
    public GeographyService geographyService;
    @Autowired
    public FileUploadUtils fileUploadUtils;
    
    
    final String IMG_PAHT_YT_GEO = "images/geo/yt/";
    final String IMG_PAHT_DA_GEO = "images/geo/da/";    





    @RequestMapping("/geographyInit") 
    public String geographyInit(Model model){
        //章节
        List<String> allZj=geographyService.getGeographyZj();
        
        model.addAttribute("allZj", allZj);
        model.addAttribute("condEntity", new GeographyEntity());
        
        return "geo/geographySearch";
    }


    @PostMapping("/geographyIns")
    public String insGeography(GeographyEntity geographyEntity, @RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile, Model model) {
        // 获取文件名
        String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
        String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();

        // 获取文件路径
        String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_GEO);
        String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_GEO);

        // 保存文件
        try {
            if (ytFile.getSize() > 0) {
                fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                geographyEntity.setImgytnm(fileName);
                geographyEntity.setImgytpath(filePath);
            }
            if (daFile.getSize() > 0) {
                fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                geographyEntity.setImgdanm(fileName2);
                geographyEntity.setImgdapath(filePath2);
            }
            // 插入数据
            geographyEntity.setFxcs(1);
            geographyEntity.setXueke(geographyEntity.getHidXueke());

            geographyService.insGeography(geographyEntity);

            // return "redirect:/geo/geographyInit";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "发生错误,请重试!");
            return "redirect:/geo/addGeography";
        }
        return "redirect:/geo/geographyInit";
    }
    @RequestMapping("/addGeography")
    public String addGeography(Model model){
        List<String> allZj=geographyService.getGeographyZj();
        
        model.addAttribute("allZj", allZj);
        return "geo/addGeography";
    }

    @RequestMapping("/searchGeography")
    public String searchGeography(Model model, @RequestParam(value = "pageNm",defaultValue= "1") Integer pageNum,
            @RequestParam(value="pageSize" ,defaultValue= "10") Integer pageSize, GeographyEntity geographyEntity) {

        PageInfo<GeographyEntity> geography = geographyService.getGeography(pageNum, pageSize, geographyEntity);
        List<String> allZj=geographyService.getGeographyZj();

        model.addAttribute("allZj", allZj);
        model.addAttribute("geography", geography);
        model.addAttribute("condEntity", geographyEntity);

        return "geo/geographySearch";
    }

    @RequestMapping("/searchGeographyByID")
    public String searchGeographyByID(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {

        List<GeographyEntity> geographyByID = geographyService.getGeographyByID(id);
        List<String> allZj=geographyService.getGeographyZj();
        if(!geographyByID.isEmpty()){
            model.addAttribute("geography", geographyByID);
        }else{
            model.addAttribute("msg", "这条记录在数据库中不存在！");
            return "geo/geographySearch";
        }
        
        model.addAttribute("allZj", allZj);

        return "geo/modifyGeography";
    }

    @PostMapping("/geographyUpd")
    public String geographyUpd(Model model, GeographyEntity geographyEntity,@RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile) {
        try {
            // 图片比较
            if(ytFile != null && ytFile.getOriginalFilename() !=null){
                if(!ytFile.getOriginalFilename().startsWith("yt", 0)){
                    // 获取文件名
                    String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_GEO);
                    if (ytFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                        geographyEntity.setImgytnm(fileName);
                        geographyEntity.setImgytpath(filePath);
                    }
                }
            }
            // 图片比较
            if(daFile != null && daFile.getOriginalFilename() !=null){
                if(!daFile.getOriginalFilename().startsWith("da", 0)){
                    // 获取文件名
                    String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_GEO);
                    if (daFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                        geographyEntity.setImgdanm(fileName2);
                        geographyEntity.setImgdapath(filePath2);
                    }                
                }
            }
            // 插入数据
            geographyEntity.setFxcs(1);
            geographyEntity.setXueke(geographyEntity.getHidXueke());

            Integer updRes=geographyService.updGeography(geographyEntity);

            if(updRes==1){
                model.addAttribute("msg", "修改成功！");
            }else{
                model.addAttribute("msg", "修改失败！");
            } 
            List<GeographyEntity> geographyByID = geographyService.getGeographyByID(geographyEntity.getId());           
            if(geographyByID.isEmpty()){
               geographyByID = new ArrayList<GeographyEntity>();
            }
            model.addAttribute("geography", geographyByID);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "文件上传发生错误,请重试!");
            
        }


        return "geo/modifyGeography";   
    }

    @PostMapping("/geographyDel")
    public String geographyDel(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {
        geographyService.delGeography(id);
        
        return "redirect:/geo/geographyInit";
    }    
    /************************************************Geography End************************************************/
}
